package com.wpen.unit.infra.repository.job.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.wpen.unit.enums.YN;
import com.wpen.unit.exception.UpdateZeroException;
import com.wpen.unit.infra.mybatis.job.mapper.TaskPlanMapper;
import com.wpen.unit.infra.mybatis.job.po.TaskPlanPo;
import com.wpen.unit.infra.mybatis.job.po.TaskPlanPo.TaskPlanColumns;
import com.wpen.unit.infra.repository.job.TaskPlanRepository;
import com.wpen.unit.utils.StringUtil;

/**
 * 任务排程维护.
 * 
 * @author pg
 *
 */
@Repository
@Transactional
public class TaskPlanRepositoryImpl extends ServiceImpl<TaskPlanMapper, TaskPlanPo> implements TaskPlanRepository {

	@Override
	public List<TaskPlanPo> queryListByBatchLogId(Long batchLogId) {
		QueryWrapper<TaskPlanPo> queryWrapper = Wrappers.query();
		queryWrapper.eq(TaskPlanColumns.BATCH_EXEC_LOG_ID.name(), batchLogId);
		return getBaseMapper().selectList(queryWrapper);
	}

	@Override
	public List<TaskPlanPo> queryListByCodes(String bizCode, String batchCode, String taskCode, Long batchTriggerTime) {
		QueryWrapper<TaskPlanPo> queryWrapper = Wrappers.query();
		queryWrapper.eq(TaskPlanColumns.BIZ_CODE.name(), bizCode);
		queryWrapper.eq(TaskPlanColumns.BATCH_CODE.name(), batchCode);
		queryWrapper.eq(TaskPlanColumns.TASK_CODE.name(), taskCode);
		queryWrapper.eq(TaskPlanColumns.BATCH_TRIGGER_TIME.name(), batchTriggerTime);
		return getBaseMapper().selectList(queryWrapper);
	}

	@Override
	public TaskPlanPo queryListByCodes(String bizCode, String batchCode, String taskCode, Long batchExecLogId,
			Long batchTriggerTime) {
		QueryWrapper<TaskPlanPo> queryWrapper = Wrappers.query();
		queryWrapper.eq(TaskPlanColumns.BIZ_CODE.name(), bizCode);
		queryWrapper.eq(TaskPlanColumns.BATCH_CODE.name(), batchCode);
		queryWrapper.eq(TaskPlanColumns.TASK_CODE.name(), taskCode);
		queryWrapper.eq(TaskPlanColumns.BATCH_EXEC_LOG_ID.name(), batchExecLogId);
		queryWrapper.eq(TaskPlanColumns.BATCH_TRIGGER_TIME.name(), batchTriggerTime);
		return getBaseMapper().selectOne(queryWrapper);
	}

	@Override
	public List<TaskPlanPo> queryAllVaild0() {
		QueryWrapper<TaskPlanPo> queryWrapper = Wrappers.query();
		queryWrapper.eq(TaskPlanColumns.VALID_STATUS.name(), YN.No.value());
		return getBaseMapper().selectList(queryWrapper);
	}

	@Override
	public List<TaskPlanPo> queryListByNextTime(int size, long nextTime) {
		QueryWrapper<TaskPlanPo> queryWrapper = Wrappers.query();
		queryWrapper.lt(TaskPlanColumns.TRIGGER_NEXT_TIME.name(), nextTime);
		queryWrapper.eq(TaskPlanColumns.VALID_STATUS.name(), YN.Yes.value());
		Page<TaskPlanPo> page = new Page<>(0, size, false);
		return super.page(page, queryWrapper).getRecords();
	}

	@Override
	public long count(TaskPlanPo taskPlanPo) {
		QueryWrapper<TaskPlanPo> queryWrapper = createQuery(taskPlanPo);
		return super.count(queryWrapper);
	}

	@Override
	public List<TaskPlanPo> queryList(int index, int size, TaskPlanPo taskPlanPo) {
		QueryWrapper<TaskPlanPo> queryWrapper = createQuery(taskPlanPo);
		if (size > 0) {
			Page<TaskPlanPo> page = new Page<>(index, size, false);
			return super.page(page, queryWrapper).getRecords();
		}
		// queryWrapper.orderBy(true, false, BatchInfoPoColumns.ID.name());
		return getBaseMapper().selectList(queryWrapper);
	}

	private QueryWrapper<TaskPlanPo> createQuery(TaskPlanPo taskPlanPo) {
		QueryWrapper<TaskPlanPo> queryWrapper = Wrappers.query();
		if (taskPlanPo.getId() != null) {
			queryWrapper.eq(TaskPlanColumns.ID.name(), taskPlanPo.getId());
		}
		if (StringUtil.isNotEmpty(taskPlanPo.getBizCode())) {
			queryWrapper.eq(TaskPlanColumns.BIZ_CODE.name(), taskPlanPo.getBizCode());
		}
		if (StringUtil.isNotEmpty(taskPlanPo.getBatchCode())) {
			queryWrapper.eq(TaskPlanColumns.BATCH_CODE.name(), taskPlanPo.getBatchCode());
		}
		if (StringUtil.isNotEmpty(taskPlanPo.getTaskCode())) {
			queryWrapper.eq(TaskPlanColumns.TASK_CODE.name(), taskPlanPo.getTaskCode());
		}
		if (taskPlanPo.getBatchTriggerTime() != null) {
			queryWrapper.eq(TaskPlanColumns.BATCH_TRIGGER_TIME.name(), taskPlanPo.getBatchTriggerTime());
		}
		if (taskPlanPo.getTriggerNextTime() != null) {
			queryWrapper.eq(TaskPlanColumns.TRIGGER_NEXT_TIME.name(), taskPlanPo.getTriggerNextTime());
		}
		if (StringUtil.isNotEmpty(taskPlanPo.getValidStatus())) {
			queryWrapper.eq(TaskPlanColumns.VALID_STATUS.name(), taskPlanPo.getValidStatus());
		}
		return queryWrapper;
	}

	@Override
	public TaskPlanPo queryFirst(TaskPlanPo taskPlanPo) {
		QueryWrapper<TaskPlanPo> queryWrapper = Wrappers.query();
		if (taskPlanPo.getId() != null) {
			queryWrapper.eq(TaskPlanColumns.ID.name(), taskPlanPo.getId());
		}
		if (StringUtil.isNotEmpty(taskPlanPo.getBizCode())) {
			queryWrapper.eq(TaskPlanColumns.BIZ_CODE.name(), taskPlanPo.getBizCode());
		}
		if (StringUtil.isNotEmpty(taskPlanPo.getBatchCode())) {
			queryWrapper.eq(TaskPlanColumns.BATCH_CODE.name(), taskPlanPo.getBatchCode());
		}
		if (StringUtil.isNotEmpty(taskPlanPo.getTaskCode())) {
			queryWrapper.eq(TaskPlanColumns.TASK_CODE.name(), taskPlanPo.getTaskCode());
		}
		if (taskPlanPo.getBatchTriggerTime() != null) {
			queryWrapper.eq(TaskPlanColumns.BATCH_TRIGGER_TIME.name(), taskPlanPo.getBatchTriggerTime());
		}
		if (taskPlanPo.getTriggerNextTime() != null) {
			queryWrapper.eq(TaskPlanColumns.TRIGGER_NEXT_TIME.name(), taskPlanPo.getTriggerNextTime());
		}
		if (StringUtil.isNotEmpty(taskPlanPo.getValidStatus())) {
			queryWrapper.eq(TaskPlanColumns.VALID_STATUS.name(), taskPlanPo.getValidStatus());
		}
		// queryWrapper.orderBy(true, false, TaskPlanPoColumns.ID.name());
		return super.getOne(queryWrapper);
	}

	@Override
	public TaskPlanPo queryById(Serializable id) {
		return super.getById(id);
	}

	@Override
	public boolean insert(TaskPlanPo taskPlanPo) {
//        taskPlanPo.setVersion(1);
//        taskPlanPo.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
		return super.save(taskPlanPo);
	}

	@Override
	public boolean insertBatch(List<TaskPlanPo> listTaskPlanPos) {
		int rs = getBaseMapper().insertBatchSomeColumn(listTaskPlanPos);
		return SqlHelper.retBool(rs);
	}

	@Override
	public boolean updateById(TaskPlanPo taskPlanPo) {
		UpdateWrapper<TaskPlanPo> updateWrapper = Wrappers.update();
		updateWrapper.eq(TaskPlanColumns.ID.name(), taskPlanPo.getId());
//        updateWrapper.eq(TaskPlanColumns.VERSION.name(), taskPlanPo.getVersion());
//        taskPlanPo.setVersion(taskPlanPo.getVersion() + 1);
		boolean rs = super.update(taskPlanPo, updateWrapper);
		// 根据ID更新所有字段，空值也更新
		// int num = getBaseMapper().alwaysUpdateSomeColumnById(BatchInfoPo);
		// boolean rs = SqlHelper.retBool(num);
		if (!rs) {
			throw new UpdateZeroException();
		}
		return rs;
	}

	@Override
	public boolean deleteByIds(Serializable[] ids) {
		UpdateWrapper<TaskPlanPo> updateWrapper = Wrappers.update();
		updateWrapper.in(TaskPlanColumns.ID.name(), (Object[]) ids);
		return super.remove(updateWrapper);
	}

//    public TaskPlanPo queryByExample(TaskPlanPo taskPlanPo) {
//        QueryWrapper<TaskPlanPo> queryWrapper = Wrappers.query();
//        if (taskPlanPo.getId() != null) {
//            queryWrapper.eq(TaskPlanColumns.ID.name(), taskPlanPo.getId());
//        }
//        if (StringUtil.isNotEmpty(taskPlanPo.getBizCode())) {
//            queryWrapper.eq(TaskPlanColumns.BIZ_CODE.name(), taskPlanPo.getBizCode());
//        }
//        if (StringUtil.isNotEmpty(taskPlanPo.getBatchCode())) {
//            queryWrapper.eq(TaskPlanColumns.BATCH_CODE.name(), taskPlanPo.getBatchCode());
//        }
//        if (StringUtil.isNotEmpty(taskPlanPo.getTaskCode())) {
//            queryWrapper.eq(TaskPlanColumns.TASK_CODE.name(), taskPlanPo.getTaskCode());
//        }
//        if (taskPlanPo.getBatchTriggerTime() != null) {
//            queryWrapper.eq(TaskPlanColumns.BATCH_TRIGGER_TIME.name(), taskPlanPo.getBatchTriggerTime());
//        }
//        if (taskPlanPo.getTriggerNextTime() != null) {
//            queryWrapper.eq(TaskPlanColumns.TRIGGER_NEXT_TIME.name(), taskPlanPo.getTriggerNextTime());
//        }
//        if (StringUtil.isNotEmpty(taskPlanPo.getValidStatus())) {
//            queryWrapper.eq(TaskPlanColumns.VALID_STATUS.name(), taskPlanPo.getValidStatus());
//        }
//		Page<TaskPlanPo> page = new Page<>(0, 1, false);
//		queryWrapper.orderBy(true, false, TaskPlanPoColumns.ID.name());
//    }

}
